package com.kid.kidliving.commodity.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kid.common.utils.PageUtils;
import com.kid.common.utils.Query;

import com.kid.kidliving.commodity.dao.CategoryDao;
import com.kid.kidliving.commodity.entity.CategoryEntity;
import com.kid.kidliving.commodity.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    //返回所有分类及其字类
    //使用java8流式计算stream api和递归操作
    @Override
    public List<CategoryEntity> listTree() {

        //查出所有列表数据
        List<CategoryEntity> listTree = baseMapper.selectList(null);
        /**  组装层级树形结构--java8 stream api + 递归
         * 过滤返回一级分类
         * 进行map映射操作，给每个分类设置对应的子分类
         * 进行排序sort操作
         * 将处理好的数据转换到集合中
        */
        List<CategoryEntity> categoryTree = listTree.stream().filter(categoryEntity -> {
            //过滤返回一级分类
            return categoryEntity.getParentId() == 0;
        }).map(categoryEntity -> {
            //进行map映射操作，给每个一级分类设置对应的子分类--递归
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity,listTree));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            //进行排序sort操作
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return categoryTree;
    }
//    递归查询所有类的子分类
    //将root分类下的所有子分类层级组织好并返回
    private List<CategoryEntity> getChildrenCategories(CategoryEntity root,List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentId() .equals( root.getId());
        }).map(categoryEntity -> {
            //进行map映射操作，给每个分类设置对应的子分类--递归
            categoryEntity.setChildrenCategories(getChildrenCategories(categoryEntity, all));
            return categoryEntity;
        }).sorted((category1, category2) -> {
            return (category1.getSort() == null ? 0 : category1.getSort()) -
                    (category2.getSort() == null ? 0 : category2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 该方法返回cascadedCategoryId，数据形式是[1,2,3]，需要使用递归
     * @param categoryId
     * @return
     */
    @Override
    public Long[] getCascadedCategoryId(Long categoryId) {

        //创建集合，将层级关系收集到集合
        List<Long> cascadedCategoryId = new ArrayList<>();
        //调用递归方法进行处理
        getParentCategoryId(categoryId,cascadedCategoryId);

        Collections.reverse(cascadedCategoryId);

        return cascadedCategoryId.toArray(new Long[cascadedCategoryId.size()]);
    }

    /**
     * 根据cascadedCategoryId递归的查找层级关系
     * 直到parentId=0
     */
    private List<Long> getParentCategoryId(Long categoryId,List<Long> cascadedCategoryId) {
        //先把categoryId放入cascadedCategoryId
        cascadedCategoryId.add(categoryId);
        //根据categoryId得到对应的CategoryEntity
        CategoryEntity categoryEntity = this.getById(categoryId);
        //判断categoryEntity的parentId是否为0,如果不为0，则说明还有上一级
        if(!categoryEntity.getParentId().equals(0L)){
            getParentCategoryId(categoryEntity.getParentId(),cascadedCategoryId);
        }
        return null;
    }

}